package com.sls.mall.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.sls.common.exception.BizCodeEnume;
import com.sls.common.exception.NoStockException;
import com.sls.mall.ware.vo.LockStockResultVo;
import com.sls.mall.ware.vo.SkuHasStockVo;
import com.sls.mall.ware.vo.WareSkuLockVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.sls.mall.ware.entity.WareSkuEntity;
import com.sls.mall.ware.service.WareSkuService;
import com.sls.common.utils.PageUtils;
import com.sls.common.utils.R;
import org.w3c.dom.stylesheets.LinkStyle;


/**
 * 商品库存
 *
 * @author Gesx
 * @email 759065243@qq.com
 * @date 2021-05-14 14:35:02
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;

    /**
     * 订单库存锁定
     * 库存解锁场景：
     *      1.下单成功，订单过期没有支付系统自动取消/或用户手动取消  需要解锁库存
     *      2.下单成功，库存锁定成功，后面的业务调用失败导致订单回滚，之前锁定的库存需要自动解锁
     * @param vo
     * @return
     */
    @PostMapping("/lock/order")
    public R orderLockStock(@RequestBody WareSkuLockVo vo){
       try {
           boolean lockStock = wareSkuService.orderLockStock(vo);
           return R.ok().setData(lockStock);
       } catch (NoStockException n){
           return R.error(BizCodeEnume.NO_STOCK_EXCEPTION.getCode(), BizCodeEnume.NO_STOCK_EXCEPTION.getMsg());
       }

    }

    /**
     * 查询有无库存
     * @param skuIds
     * @return
     */
    @PostMapping("/hasstock")
    public R getSkusHasStock(@RequestBody List<Long> skuIds){
        List<SkuHasStockVo> skusHasStock = wareSkuService.getSkusHasStock(skuIds);

        return R.ok().setData(skusHasStock);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("ware:waresku:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wareSkuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("ware:waresku:info")
    public R info(@PathVariable("id") Long id){
		WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("ware:waresku:save")
    public R save(@RequestBody WareSkuEntity wareSku){
		wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("ware:waresku:update")
    public R update(@RequestBody WareSkuEntity wareSku){
		wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("ware:waresku:delete")
    public R delete(@RequestBody Long[] ids){
		wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
